package com.cloud.shopping.upload.controller;

import com.alibaba.fastjson.JSON;
import com.cloud.shopping.tool.RespResult;
import com.cloud.shopping.upload.qiniu.QiNiuConfig;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author John.zhang
 * @version Id: UploadController, v 1.0 2020/12/28 15:22 ZhiYong Exp $
 */

@RestController
@RequestMapping(value = "/file")
public class UploadController {

    @Resource
    private UploadManager uploadManager;

    @PostMapping(value = "/upload")
    public RespResult upload(MultipartFile file) throws IOException {
        String key = file.getOriginalFilename();
        Response response = uploadManager.put(file.getBytes(), key, new QiNiuConfig().getToken());
        DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
        return RespResult.ok(putRet.key);
    }


    @GetMapping(value = "/download/{filename}")
    public void download(@PathVariable String filename, HttpServletResponse response) {


    }

}
